<?php
include("VerifySession.php");
?>

<?php
include("Header.php");
include("SNManager.class.php");
$db = new SNDataManager();
$is_set_file=false;
if(isset($_POST["Import"]))
{
	$is_set_file =true;
}
?>
<!-- Article -->
<div id="adbox">
                <h2><span>Import Result</span></h2>
                <p>
<?php if(!$is_set_file) { ?>
          	 <table width="1024" border="0" align="center">
              <tr>
                <td>&nbsp;</td>
                <td align="center"><a href="ImportFilePage.php">Invalid File:Please Upload File</a></td>
                <td>&nbsp;</td>
              </tr>
            </table> 
          <?php } else { ?>
          <table width="600" border="0" align="center">
          <tr>
                <td align="center">Serial Number</td>
                <td align="center">Generate Date</td>
                <td align="center">Result</td>
               </tr>
<?php
    $filename=$_FILES["file"]["tmp_name"];
    if($_FILES["file"]["size"] > 0)
    {
        $file = fopen($filename, "r");
        while(! feof($file))
        {
            $emapData = fgetcsv($file,100,'|');
            //echo $emapData;
            $no=$emapData[0];
            if(!$db->IsNullOrEmpty($no))
            {
                $dateDE = $emapData[1];
                $dupicate =false;
                
                $dateObj =  date_create($dateDE);
                if (!$dateObj)
                {
                    $dateObj = getdate();
                }
                
               $findResult=  $db->CheckDupicateSNNumber($no);
               $dupicate = mysql_num_rows($findResult)>0;
                ?>
                  <tr>
                    <td><?php echo $no; ?> </td>
                    <td><?php echo date_format($dateObj,"Y/m/d"); ?></td>
              <?php 
                if($dupicate)
                { ?>
                   <td>Dupicate</td>                 
               <?php  
                } 
                else 
                { 
                  $result= $db->ImportSNNumber($no,$dateObj,$_SESSION["AccountID"]);
                  ?>
                  <td><?php  if($result){ echo "Success";} else { echo "Failed";} ?></td>
               <?php 
                }
            }
            ?>
          </tr>
<?php   
        }
        fclose($file);
    }	
?>
 </table> 
 
 <table width="600" border="0" align="center">
      <tr>
        <td>&nbsp;</td>
        <td align="center"><a href="ImportFilePage.php">Click here to ImportFile Page.</a></td>
        <td>&nbsp;</td>
      </tr>
    </table>
    
 <?php } ?>               
</p>
 </div> <!-- /article --><!-- Article -->  
 <?php
   include("Footer.php");
?>